#include "../app_status_bar_conf.h"
#include "../inc/app_status_bar_type.h"

#if STATUS_BAR_APP_USE


static app_status_bar_scr_objs_t *scr_objs = NULL;
extern app_status_bar_scr_objs_t *app_status_bar_get_objs();

#if STATUS_BAR_USE_WIFI
static const char *wifi_list[] = {STATUS_BAR_SYMBOL_WIFI_0, STATUS_BAR_SYMBOL_WIFI_1, STATUS_BAR_SYMBOL_WIFI_2, STATUS_BAR_SYMBOL_WIFI_3, STATUS_BAR_SYMBOL_WIFI_4};
#endif

#if STATUS_BAR_USE_TIME
static const char *week_list[] = {"日", "一", "二", "三", "四", "五", "六"};
#if STATUS_BAR_USE_TOPIC_MESSENGER
static void time_update_cb(void *arg, size_t size)
{
    uint8_t *time = (uint8_t *)arg; // 年月日周时分秒 0 - 6
    lv_label_set_text_fmt(scr_objs->time, "%02d:%02d", time[4], time[5]);
    lv_label_set_text_fmt(scr_objs->date, "%d月%d日 周%s", time[1], time[2], week_list[time[3]]);
}
#else
void status_bar_set_time(uint8_t year, uint8_t month, uint8_t day, uint8_t week, uint8_t hour, uint8_t minute, uint8_t second)
{
    lv_label_set_text_fmt(scr_objs->time, "%02d:%02d", hour, minute);
    lv_label_set_text_fmt(scr_objs->date, "%d月%d日 周%s", month, day, week_list[week]);
}
#endif // STATUS_BAR_USE_TOPIC_MESSENGER
#endif // STATUS_BAR_USE_TIME

#if STATUS_BAR_USE_BATTERY
#if STATUS_BAR_USE_TOPIC_MESSENGER
static void soc_update_cb(void *arg, size_t size)
{
    uint8_t soc = *(uint8_t *)arg;
#else
void status_bar_set_soc(uint8_t soc)
{
#endif
    lv_bar_set_value(lv_obj_get_child(scr_objs->bat, 0), soc, LV_ANIM_OFF);

    if (soc < STATUS_BAR_BAT_LOW_LEVEL)
    {
#if STATUS_BAR_USE_BATTERY_CHARGE
        if (lv_obj_has_flag(scr_objs->charge, LV_OBJ_FLAG_HIDDEN))
#endif
            lv_obj_set_style_bg_color(lv_obj_get_child(scr_objs->bat, 0), lv_color_hex(0xFF0500), LV_PART_INDICATOR);
    }
    else
    {
#if STATUS_BAR_USE_BATTERY_CHARGE
        if (lv_obj_has_flag(scr_objs->charge, LV_OBJ_FLAG_HIDDEN))
#endif
            lv_obj_set_style_bg_color(lv_obj_get_child(scr_objs->bat, 0), lv_color_hex(0x878787), LV_PART_INDICATOR);
    }

#if STATUS_BAR_USE_BATTERY_NUMBER
    lv_label_set_text_fmt(lv_obj_get_child(scr_objs->bat, 1), "%d", soc);
#endif // STATUS_BAR_USE_BATTERY_NUMBER
}
#endif // STATUS_BAR_USE_BATTERY
#if STATUS_BAR_USE_BATTERY_CHARGE
static void charge_sta_cb(void *arg, size_t size)
{
    uint8_t sta = *(uint8_t *)arg;
    if (sta)
    {
        lv_obj_clear_flag(scr_objs->charge, LV_OBJ_FLAG_HIDDEN);
#if STATUS_BAR_USE_BATTERY
        lv_obj_set_style_bg_color(lv_obj_get_child(scr_objs->bat, 0), lv_color_hex(0x00A000), LV_PART_INDICATOR);
#endif
    }
    else
    {
        lv_obj_add_flag(scr_objs->charge, LV_OBJ_FLAG_HIDDEN);

#if STATUS_BAR_USE_BATTERY
        uint8_t soc = lv_bar_get_value(lv_obj_get_child(scr_objs->bat, 0));
        if (soc < STATUS_BAR_BAT_LOW_LEVEL)
            lv_obj_set_style_bg_color(lv_obj_get_child(scr_objs->bat, 0), lv_color_hex(0xFF0500), LV_PART_INDICATOR);
        else
            lv_obj_set_style_bg_color(lv_obj_get_child(scr_objs->bat, 0), lv_color_hex(0x878787), LV_PART_INDICATOR);
#endif
    }
}
#if (!STATUS_BAR_USE_TOPIC_MESSENGER && STATUS_BAR_USE_BATTERY_CHARGE)
void status_bar_set_charge_state(bool state)
{
    charge_sta_cb(&state, 1);
}
#endif
#endif // STATUS_BAR_USE_BATTERY_CHARGE

#if STATUS_BAR_USE_WIFI
#if STATUS_BAR_USE_TOPIC_MESSENGER
static void wifi_rssi_cb(void *arg, size_t size)
{
    uint8_t level = *(uint8_t *)arg;
#else
void status_bar_set_wifi_rssi_level(uint8_t level)
{
#endif

    if (level > 4)
    {
        lv_obj_add_flag(scr_objs->wifi, LV_OBJ_FLAG_HIDDEN);
    }
    else
    {
        lv_obj_clear_flag(scr_objs->wifi, LV_OBJ_FLAG_HIDDEN);
        lv_label_set_text_static(scr_objs->wifi, wifi_list[level]);
    }
}
#endif // STATUS_BAR_USE_WIFI

#if STATUS_BAR_USE_BT
#if STATUS_BAR_USE_TOPIC_MESSENGER
static void bt_sta_cb(void *arg, size_t size)
{
    if (*(uint8_t *)arg)
        lv_obj_clear_flag(scr_objs->bt, LV_OBJ_FLAG_HIDDEN);
    else
        lv_obj_add_flag(scr_objs->bt, LV_OBJ_FLAG_HIDDEN);
}
#else
void status_bar_set_bt_state(bool state)
{
    bt_sta_cb(&state, 1);
}
#endif // STATUS_BAR_USE_TOPIC_MESSENGER
#endif // STATUS_BAR_USE_BT

void app_status_bar_widgets_init(void)
{
    scr_objs = app_status_bar_get_objs();

#if STATUS_BAR_USE_TIME
    uint8_t time[7] = {25, 3, 8, 0, 02, 13, 0};
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取时间", time, sizeof(time));
#endif // STATUS_BAR_USE_TOPIC_MESSENGER
    scr_objs->time = lv_label_create(scr_objs->bar_left);
    lv_label_set_text_fmt(scr_objs->time, "%02d:%02d", time[4], time[5]);
    lv_obj_set_style_text_font(scr_objs->time, &app_status_bar_font_time, 0);

    lv_obj_t *cont = lv_obj_create(lv_layer_sys());
    lv_obj_set_style_bg_opa(cont, 0, 0);
    lv_obj_set_style_border_width(cont, 0, 0);
    lv_obj_set_style_radius(cont, 0, 0);
    lv_obj_clear_flag(cont, LV_OBJ_FLAG_CLICKABLE);

    scr_objs->date = lv_label_create(cont);
    lv_obj_align_to(scr_objs->date, scr_objs->time, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 5);
    lv_label_set_text_fmt(scr_objs->date, "%d月%d日 周%s", time[1], time[2], week_list[time[3]]);
    lv_obj_set_style_text_font(scr_objs->date, &app_status_bar_font_date, 0);
    lv_obj_add_flag(cont, LV_OBJ_FLAG_HIDDEN);
#endif // STATUS_BAR_USE_TIME

#if STATUS_BAR_USE_BATTERY_CHARGE
    scr_objs->charge = lv_label_create(scr_objs->bar_right);
    lv_label_set_text_static(scr_objs->charge, STATUS_BAR_SYMBOL_CHARGE);
    lv_obj_set_style_text_font(scr_objs->charge, &app_status_bar_font_charge, 0);
    lv_obj_add_flag(scr_objs->charge, LV_OBJ_FLAG_HIDDEN);
#endif // STATUS_BAR_USE_BATTERY_CHARGE

#if STATUS_BAR_USE_BATTERY
    uint8_t soc;
#if STATUS_BAR_USE_TOPIC_MESSENGER
    topic_messenger_msg("获取电量", &soc, sizeof(soc));
#endif // STATUS_BAR_USE_TOPIC_MESSENGER

    printf("soc: %d\n", soc);

    scr_objs->bat = lv_label_create(scr_objs->bar_right);
    lv_label_set_text_static(scr_objs->bat, STATUS_BAR_SYMBOL_BAT);
    lv_obj_set_style_text_font(scr_objs->bat, &app_status_bar_font_bat, 0);
    lv_obj_set_width(scr_objs->bat, 24);

    lv_obj_t *bar = lv_bar_create(scr_objs->bat);
    lv_bar_set_range(bar, 0, 100);
    lv_obj_set_size(bar, STATUS_BAR_BAT_WIDTH, STATUS_BAR_BAT_HEIGHT);
    lv_bar_set_value(bar, soc, LV_ANIM_OFF);
    lv_obj_set_style_bg_color(bar, lv_color_hex(0x878787), LV_PART_INDICATOR);
    lv_obj_set_style_radius(bar, 0, 0);
    lv_obj_set_style_radius(bar, 0, LV_PART_INDICATOR);
    lv_obj_align(bar, LV_ALIGN_CENTER, STATUS_BAR_BAT_X_OFFSET, 0);
    lv_obj_clear_flag(bar, LV_OBJ_FLAG_CLICKABLE);

#if STATUS_BAR_USE_BATTERY_NUMBER

    lv_obj_t *soc_label = lv_label_create(scr_objs->bat);
    lv_label_set_text_fmt(soc_label, "%d", soc);
    lv_obj_set_style_text_font(soc_label, &app_status_bar_font_soc, 0);
    lv_obj_align(soc_label, LV_ALIGN_CENTER, STATUS_BAR_BATTERY_NUMBER_X_OFFSET, 0);

#endif // STATUS_BAR_USE_BATTERY_NUMBER
#endif // STATUS_BAR_USE_BATTERY

#if STATUS_BAR_USE_WIFI
    scr_objs->wifi = lv_label_create(scr_objs->bar_right);
    lv_obj_set_style_text_font(scr_objs->wifi, &app_status_bar_font_wifi, 0);
    // lv_img_set_src(scr_objs->wifi, &app_status_bar_img_wifi1);
    lv_obj_add_flag(scr_objs->wifi, LV_OBJ_FLAG_HIDDEN);
#endif // STATUS_BAR_USE_WIFI

#if STATUS_BAR_USE_BT
    scr_objs->bt = lv_label_create(scr_objs->bar_right);
    lv_obj_set_style_text_font(scr_objs->bt, &app_status_bar_font_bt, 0);
    lv_label_set_text_static(scr_objs->bt, STATUS_BAR_SYMBOL_BT);
    lv_obj_add_flag(scr_objs->bt, LV_OBJ_FLAG_HIDDEN);
#endif // STATUS_BAR_USE_BT

#if STATUS_BAR_USE_TOPIC_MESSENGER
#if STATUS_BAR_USE_TIME
    topic_messenger_subscribe(NULL, "时间更新", time_update_cb);
#endif // STATUS_BAR_USE_TIME
#if STATUS_BAR_USE_BATTERY
    topic_messenger_subscribe(NULL, "电量更新", soc_update_cb);
#endif // STATUS_BAR_USE_BATTERY
#if STATUS_BAR_USE_BATTERY_CHARGE
    topic_messenger_subscribe(NULL, "充电状态更新", charge_sta_cb);
#endif // STATUS_BAR_USE_BATTERY_CHARGE
#if STATUS_BAR_USE_WIFI
    topic_messenger_subscribe(NULL, "WIFI信号更新", wifi_rssi_cb);
#endif // STATUS_BAR_USE_WIFI
#if STATUS_BAR_USE_BT
    topic_messenger_subscribe(NULL, "蓝牙信号更新", bt_sta_cb);
#endif // STATUS_BAR_USE_BT
#endif // STATUS_BAR_USE_TOPIC_MESSENGER
}

#if STATUS_BAR_USE_TIME
static void label_set_opa(lv_obj_t *obj, lv_coord_t opa)
{
    lv_obj_set_style_text_opa(obj, opa, 0);

    if (opa == 0)
        lv_obj_add_flag(lv_obj_get_parent(scr_objs->date), LV_OBJ_FLAG_HIDDEN);
}
#endif

void app_status_bar_expand_widgets_cb() // 展开回调
{
#if STATUS_BAR_USE_TIME
    lv_obj_clear_flag(lv_obj_get_parent(scr_objs->date), LV_OBJ_FLAG_HIDDEN);
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, scr_objs->date);
    lv_anim_set_values(&a, 1, 255);
    lv_anim_set_time(&a, 200);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)label_set_opa);
    lv_anim_start(&a);
#endif
}

void app_status_bar_hide_widgets_cb() // 关闭回调
{
#if STATUS_BAR_USE_TIME
    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, scr_objs->date);
    lv_anim_set_values(&a, 255, 0);
    lv_anim_set_time(&a, 500);
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)label_set_opa);
    lv_anim_start(&a);
#endif
}

#endif

